import { EvnetBus } from '@/eventBus/index'
import vuex from '@/store'
import LoginView from '@/views/login/LoginView.vue'
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: "登录",
    component: LoginView
  },
  {
    path: '/index',
    name: '主页面',
    component: () => import('../views/examIndex/ExamIndexVue.vue'),

    redirect: "/examIndex",
    meta: {
      required: true
    },
    children: [{
      name: "首页",
      path: "/examIndex",
      component: () => import('../views/examIndex/ExamindexView.vue'),
      meta: {
        bread: true,
      },
    },
    {
      path: '/user',
      name: '用户管理',
      component: () => import('../views/user/UserView.vue'),
      meta: {
        bread: true,
      },
      children: [
        {
          path: "/user/student/list",
          name: "学生列表",
          component: () => import('../views/user/studentList/StudentView.vue'),

          meta: {
            bread: true,
          },
        },
        {
          path: "/user/add/list",
          name: "编辑页面",
          component: () => import("../views/exam/paper/addView.vue"),
          meta: {
            bread: true
          },
        },
        {
          path: "/exam/add/list",
          name: "编辑页面",
          component: () => import("../views/exam/paper/addView.vue"),
          meta: {
            bread: true,
          }
        },
        {
          path: "/user/student/listbj",
          name: "学生编辑",
          component: () => import('../views/user/studentList/SetstudentView.vue'),
        },
        {
          name: "管理员列表",
          path: "/user/admin/list",
          component: () => import('../views/user/adminList/AdminView.vue'),

          meta: {
            bread: true,
          },
        }
      ]
    },
    {
      path: '/exam',
      name: '答卷管理',
      component: () => import('../views/exam/ExamView.vue'),
      meta: {
        bread: true,
      },
      children: [
        {
          path: "/exam/paper/list",
          name: "试卷列表",
          component: () => import('../views/exam/paper/PaperView.vue'),
          meta: {
            bread: true,
          },
        },
        {
          name: "题目列表",
          path: "/exam/question/list",
          component: () => import('../views/exam/question/QuestionView.vue'),
          meta: {
            bread: true,
          },
        },
        {
          path: "/exam/Single/list",
          name: "单选题编辑",
          component: () => import("../views/exam/question/Fiveages/SingleView.vue"),
          meta: {
            bread: true,
          },
        },
        {
          path: "/exam/Multiple/list",
          name: "多选题编辑",
          component: () => import("../views/exam/question/Fiveages/MultipleView.vue"),
          meta: {
            bread: true,

          },
        },
        {
          path: "/exam/Judgment/list",
          name: "判断题编辑",
          component: () => import("../views/exam/question/Fiveages/JudgmentView.vue"),
          meta: {
            bread: true,
          },
        },
        {
          path: "/exam/Fillinthe/list",
          name: "填空题编辑",
          component: () => import("../views/exam/question/Fiveages/FillintheView.vue"),
          meta: {
            bread: true,
          },
        },
        {
          path: "/exam/Short/list",
          name: "简答题页面",
          component: () => import("../views/exam/question/Fiveages/ShortView.vue"),
          meta: {
            bread: true,
          },
        }
      ]
    },
    {
      path: '/task',
      name: '任务管理',
      component: () => import('../views/task/TaskView'),
      meta: {
        bread: true,
      },
      children: [
        {
          path: "/task/list",
          name: "任务列表",
          component: () => import('../views/task/tasklist/TaskListView.vue'),
          meta: {
            bread: true,
          },
        },
        {
          name: "任务创建",
          path: "/task/edit",
          component: () => import('../views/task/taskestablish/TaskEstablishView.vue'),
          meta: {
            bread: true,
          },
        }
      ]
    },
    {
      path: '/answer',
      name: '成绩管理',
      component: () => import('../views/performance/PerformanceView'),
      meta: {
        bread: true,
      },
      children: [
        {
          path: "/answer/list",
          name: "答卷列表",
          component: () => import('../views/performance/answerList/AnswerListView.vue'),
          meta: {
            bread: true,
          },
        }
      ]
    },
    {
      path: '/message',
      name: '消息中心',
      component: () => import('../views/word/WordView'),
      meta: {
        bread: true,
      },
      children: [
        {
          path: "/message/list",
          name: "消息列表",
          component: () => import('../views/word/wordList/WordListView.vue'),
          meta: {
            bread: true,
          },
        },
        {
          name: "消息发送",
          path: "/message/send",
          component: () => import('../views/word/wordSend/WordSendView.vue'),
          meta: {
            bread: true,
          },
        }
      ]
    },
    {
      path: '/education',
      name: '教育管理',
      component: () => import('../views/education/EducationView.vue'),
      meta: {
        bread: true
      },
      children: [
        {
          path: '',
          redirect: ""
        },
        {
          path: '/education/subject',
          name: '学科列表',
          component: () => import('../views/education/subject/EducationSubject.vue'),
        },
        {
          path: '/education/redact',
          name: '学科编辑',
          component: () => import('../views/education/subject/EducationRedact'),
        },
      ]
    },
    {
      path: '/answer',
      name: '成绩管理',
      component: () => import('../views/performance/PerformanceView'),
      children: [
        {
          path: "/answer/list",
          name: "答卷列表",
          component: () => import('../views/performance/answerList/AnswerListView.vue'),
        }
      ]
    },
    {
      path: '/message',
      name: '消息中心',
      component: () => import('../views/word/WordView'),
      children: [
        {
          path: "/message/list",
          name: "消息列表",
          component: () => import('../views/word/wordList/WordListView.vue'),
        },
        {
          name: "消息发送",
          path: "/message/send",
          component: () => import('../views/word/wordSend/WordSendView.vue'),
        }
      ]
    },
    {
      path: '/journal',
      name: '日志中心',
      component: () => import('../views/journal/JournalView'),
      children: [
        {
          name: "用户日志",
          path: "/journal/list",
          component: () => import('../views/journal/userJournal/UserJournalView.vue'),
        }
      ]
    },
    {
      path: '/profile',
      name: '个人中心',
      component: () => import('../views/individual/IndividualView'),
      meta: {
        bread: true
      }
    }
    ]
  }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes
})
router.beforeEach(function (to, from, next) {
  let flag = to.matched.some(item => item.meta.required)
  if (flag) {
    if (localStorage.getItem("token")) {
      next()
    } else {
      next("/")
    }
  }
  if (to.meta.bread) {
    vuex.commit('changeBread', {
      title: to.name,
      src: to.path,
      name: Number(vuex.state.tabIndex) + 1 + '',
    });
  };
  EvnetBus.$emit("routeChange", to);
  vuex.commit('changeTop', to.matched);

  window.sessionStorage.setItem("route", to.path)
  next();
})

export default router
